package com.zyh.util;


import org.junit.Test;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;

public class SocketUtils {
    public static String sendData(String xmldata) {
        //获取字节数组的8位数字
        byte[] bytes = xmldata.getBytes();
        String checkNum = String.format("%08d", bytes.length);
        String body = checkNum + xmldata;
        System.out.println("发送的xml:" + body);

        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader br = null;
        Properties properties = new Properties();
        //接受返回数据的缓冲区
        StringBuffer backData = new StringBuffer();
        String ip = ConfigInfo.getConfigInfoByName("esb.server.ip");
        String port = ConfigInfo.getConfigInfoByName("esb.server.port");
        try {
            //建立TCP连接
            socket = new Socket();

            socket.setSoTimeout(50000);
            socket.connect(new InetSocketAddress(ip,port == null ? 22222 : Integer.parseInt(port)),10000);
            //写入数据
            outputStream = socket.getOutputStream();
            outputStream.write(body.getBytes("UTF-8"));
            System.out.println("数据写入完毕");

            //获取响应
            inputStream = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            String info = null;
            while ((info = br.readLine()) != null) {
//                logger.info(info);
                backData.append(info);
                System.out.println("返回数据");
                System.out.println(backData.toString());
            }
            br.close();
            inputStream.close();
            outputStream.close();

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
            System.out.println("创建连接失败");
        } catch (IOException e2) {
            System.out.println("socket连接被拒绝");
            e2.printStackTrace();
        }finally{
            try {
                if(socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //返回的ESB响应XML，应该去掉前面的8位数字
        String backXml = backData.toString();
        if(backXml!=null&&backXml.length()>8){
            backXml = backXml.substring(8,backXml.length());
        }
        return backXml;
    }



    @Test
    public  void Test(){

        String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<service><SysHead><SvcCd>300130001</SvcCd><ScnCd>23</ScnCd><CnsmrSysNo>300800</CnsmrSysNo><BsnSrlNo>C4012020201210142932792944326440</BsnSrlNo><TxnDt>20201210</TxnDt><TxnTm>20201210</TxnTm></SysHead><AppHead><CnlCd>C30320</CnlCd><InstNo>003</InstNo><OtltsNo>10001</OtltsNo><UsrNo>8170004</UsrNo></AppHead><LocalHead><TlrTp>0</TlrTp><AuthTlrNo>0000000</AuthTlrNo><OldAcctFlg>1</OldAcctFlg></LocalHead><Body><ProvCd>43</ProvCd><OldAcctNo/><OldAcctOfstLo>29</OldAcctOfstLo><AcctNo>82010000000364544</AcctNo><SubAcctCtgry/><SubAcctVlmNo/><SubAcctSeqNo>0</SubAcctSeqNo><TxnBegDt>01102010</TxnBegDt><TxnEndDt>05122020</TxnEndDt></Body></service>";

        String responseXml = sendData(xmlData);
        System.out.println(responseXml);
    }
}

